<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2022/3/29
 * Time: 11:18
 */

namespace app\project\service;

use app\common\controller\Common;
use think\Config;
use think\Db;
use think\Session;
class PublicService
{
    //列表
    static function ListService($table,$where,$field=null,$page=0,$limit=0,$order="id desc"){
        $common = new Common();
        $start = 0;
        if($page!=0){
            $start = ($page-1)*$limit;
        }
        $list = $common->public_select($table,$where,$field,$start,$limit)->order($order)->select();
        if($page!=0){
            $count = $common->public_select($table,$where)->count('*');
            $black['count'] = $count;
            $black['list'] = $list;
        }else{
            $black = $list;
        }
        return $black;
    }

    //详情
    static function detailService($table,$where,$type=0,$order="id desc"){
        $common = new Common();
        $detail = $common->public_select($table,$where)->order($order)->find();
        if(empty($detail) & empty($type)){
            $showTable = "us_".$table;
            $queryTable = Db::query("show columns from ".$showTable); //获取表结构
            foreach ($queryTable as $item) {
                $detail[$item['Field']] = "";
            };
        }
        return $detail;
    }

    //统计操作
    static function counService($table,$where){
        $common = new Common();
        $count = $common->public_select($table,$where)->count('*');
        return $count;
    }

    //保存操作
    static function findCounService($table,$where,$field){
        $common = new Common();
        $count = $common->public_select($table,$where)->sum($field);
        return empty($count)?0:$count;
    }

    //保存操作
    static function saveService($table,$data,$type=1){
        $common = new Common();
        return $common->handle($table,$data,$type);
    }

    //删除操作
    static function delService($table,$where){
        $common = new Common();
        $common->public_select($table,$where)->delete();
    }
}